package com.dbf.site.server;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.dbf.forum.common.service.CommonService;
import com.dbf.forum.core.Message;
import com.dbf.site.client.message.MessageService;
import com.dbf.site.client.message.model.MessageModel;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.sencha.gxt.data.shared.loader.PagingLoadConfig;
import com.sencha.gxt.data.shared.loader.PagingLoadResult;
import com.sencha.gxt.data.shared.loader.PagingLoadResultBean;

public class MessageServiceImpl  extends RemoteServiceServlet implements
MessageService {
	/**
	 * 
	 */
	private static final long serialVersionUID = -7900842993778611806L;

	public PagingLoadResult<MessageModel> getMessages(PagingLoadConfig loadConfig){
		ApplicationContext context = null;
		try
		{
		context = new ClassPathXmlApplicationContext("main-context.xml");
		}catch(Exception e){
			e.printStackTrace();
		}
	    CommonService commonsService = (CommonService)context.getBean("CommonServiceImpl", CommonService.class);
		List<Message> messages = commonsService.getAllEntities(Message.class);
		List<MessageModel> messageModels = new ArrayList<MessageModel>();
		for (Message message : messages) {
			MessageModel messageModel = new MessageModel();
			messageModel.setId(message.getId());
			messageModel.setMessageComment(message.getMessageComment());
			messageModel.setMessageCreationDate(message.getMessageCreationDate());
			messageModels.add(messageModel);
		}

		PagingLoadResult<MessageModel>  pagingLoadResult = new PagingLoadResultBean<MessageModel>(messageModels, messageModels.size(), 10);
		return pagingLoadResult;
		
	}

}
